package com.xiaoxie.report.mapper.typehandler;

import com.xiaoxie.common.utils.JSONUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

// @MappedJdbcTypes(JdbcType.VARCHAR)
// @MappedTypes(Map.class)
public class MapTypeHandler /*extends BaseTypeHandler<Map<String,String>>*/ {



    public void setNonNullParameter(PreparedStatement ps, int i, Map<String, String> parameter, JdbcType jdbcType) throws SQLException {
        String json = JSONUtils.toJson(parameter);
        ps.setString(i, json);
    }


    public Map<String, String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String json = rs.getString(columnName);
        if (json == null) {
            return null;
        }
        return JSONUtils.toBean(json, Map.class);
    }


    public Map<String, String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String json = rs.getString(columnIndex);
        if (json == null) {
            return null;
        }
        return JSONUtils.toBean(json, Map.class);
    }


    public Map<String, String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String json = cs.getString(columnIndex);
        if (json == null) {
            return null;
        }
        return JSONUtils.toBean(json, Map.class);
    }
}
